// packageUser/pages/evaluate/evaluate.js
import OrderService from '../../../service/OrderService';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    evaluate: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.orderService = new OrderService();
    this.setFieldValue("orderId", options.orderId);
  },
  setFieldValue:function(field, value){
    const { evaluate } = this.data;
    evaluate[field] = value;
    this.setData({ evaluate });
  },
  onContentChange: function (e) {
    this.setFieldValue("content", e.detail.value);
  },
  onImagesChange: function (e) {
    const images = e.detail.images;
    const fileList = images.map(item => {
      return { fileUrl: item };
    })
    this.setFieldValue("fileList", fileList);
  },
  validForm: function(form){
    if (!form.orderId) {
      this.setData({ errMsg: '订单ID为空' })
      return false;
    }
    if (!form.content) {
      this.setData({ errMsg: '评价内容不能为空' })
      return false;
    }
    this.setData({ errMsg: '' })
    return true;
  },
  submit: function (e) {
    const { evaluate } = this.data;
    if (!this.validForm(evaluate)) {
      return;
    }
    evaluate.type = 0;
    evaluate.source = app.globalData.user.userType;
    this.orderService.evaluate(evaluate).then(function (res) {
      if (res.status == 0) {
        const { evalScore } = app.globalData.setting;
        _.commonService.active(6).then(function (res) {
          if (res.status == 0) {
            wx.showModal({
              title: '评论成功',
              content: `你获得${evalScore}枚救兵勋章`,
              showCancel: false,
              confirmText: '知道了',
              confirmColor: '#0f9bc1',
              success: function (res) {
                if (res.confirm) {
                  wx.navigateBack()
                }
              }
            })
          }
        })
      }
    })
  }
})